﻿#ifndef _VECTOR_FEATURE_CLASS_H_H_
#define _VECTOR_FEATURE_CLASS_H_H_

#include "udatabase/dll_cfg.h"
#include "udatabase/interface/ifeatureclass2.h"
#include "graphicengine/OgreCommon.h"

namespace FD{
	namespace Utility{
		namespace DB{

class CVectorFeatureClassOperator;
class CVectorFeatureClassEditor;
class CFeatureIterator;
/// <summary>
/// 二维矢量要素集类
/// </summary>
class FD_DB_Export CVectorFeatureClass : public FD::Utility::DB::Interface::IFeatureClass2
{
	friend class CFeatureIterator;
public:
	/// <summary>
	/// 默认析构函数
	/// </summary>
		~CVectorFeatureClass();
public:
	/// <summary>
	/// 获取数据集类型
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集类型</returns>
	virtual FD::Utility::DB::Interface::DatasetType getType() const;
	/// <summary>
	/// 获取数据集名称
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集名称</returns>
	virtual String getName() const;
	/// <summary>
	/// 获取数据集版本号，该方法是确认数据变更情况。
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集版本号</returns>
	virtual int64 getDataVersion() const;
	/// <summary>
	/// 获取数据集描述
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集描述</returns>
	virtual String getDescription() const;
	/// <summary>
	/// 获取数据集更新时间
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集更新时间</returns>
	virtual String getUpdateTime() const;
	/// <summary>
	/// 判断是否包含子数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>是，返回true;否则，返回false</returns>
	virtual bool hasSubDataset() const;
	/// <summary>
	/// 获取数据集所在数据源对象，该值为引用对象指针。
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集所在数据源对象指针</returns>
	virtual FD::Utility::DB::Interface::IDataSource* getDataSourceRef() const;
	/// <summary>
	/// 判断数据集是否具有编辑能力
	/// </summary>
	/// <param name=""></param>
	/// <returns>是，返回true;否则，返回false</returns>
	virtual bool canEdit() const;
	/// <summary>
	/// 判断数据集是否处在编辑状态
	/// </summary>
	/// <param name=""></param>
	/// <returns>是，返回true;否则，返回false</returns>
	virtual bool isEditing() const;
	/// <summary>
	/// 开始编辑，用于标识数据集处在编辑状态，并根据参数，设置相关编辑对象。
	/// </summary>
	/// <param name="withUndo">确定是否开启Undo功能</param>
	/// <returns>开启编辑成功，返回true；否则，返回false</returns>
	virtual bool beginEditing(bool withUndo);
	/// <summary>
	/// 结束编辑，用于标识数据集结束编辑，根据参数，确实是否保存数据。
	/// </summary>
	/// <param name="isSave">需要保存编辑数据，设置true;否则，设置false</param>
	/// <returns>结束编辑成功，返回true;否则，返回false</returns>
	virtual bool endEditing(bool isSave);
	/// <summary>
	/// 判断数据集是否处在编辑操作状态
	/// </summary>
	/// <param name=""></param>
	/// <returns>是，返回true;否则，返回false</returns>
	virtual bool isBeginEditingOperation();
	/// <summary>
	/// 开启编辑操作，用于标识数据集编辑操作。
	/// 在调用endEditingOperation操作后，把两次操作之间的编辑，作为一次编辑状态提交。
	/// </summary>
	/// <param name=""></param>
	/// <returns>开启编辑操作成功，返回true;否则，返回false</returns>
	virtual bool beginEditingOperation();
	/// <summary>
	/// 结束编辑操作，用于标识数据集编辑操作结束。
	/// 在调用endEditingOperation操作后，把两次操作之间的编辑，作为一次编辑状态提交。
	/// </summary>
	/// <param name="isConfirm">提交操作，设置true;否则，设置false</param>
	/// <returns>结束编辑操作成功，返回true;否则，返回false</returns>
	virtual bool endEditingOperation(bool isConfirm);


public:
	/// <summary>
	/// 获取要素集类型
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集类型</returns>
	virtual FD::Utility::DB::Interface::FeatureClassType	getFeatureClassType() const 
	{return  FD::Utility::DB::Interface::FCT_VECTORFEATURECLASS;}
	/// <summary>
	/// 获取要素集几何体类型
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集几何体类型</returns>
	virtual FD::Kernel::Geometry::Interface::GeometryType		getGeometryType() const;
	/// <summary>
	/// 获取要素集字段信息
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集字段信息</returns>
	virtual const FD::Utility::DB::Interface::IFields*		getFieldsRef() const;
	/// <summary>
	/// 获取要素集字段数量
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集字段数量</returns>
	virtual uint32			getFieldCount() const;
	/// <summary>
	/// 通过索引获取字段
	/// </summary>
	/// <param name="index">索引处</param>
	/// <returns>返回索引处要素集字段</returns>
	virtual const FD::Utility::DB::Interface::IField*		getFieldRef(uint32 index) const;
	/// <summary>
	/// 通过名称查找字段
	/// </summary>
	/// <param name="fieldName">字段名</param>
	/// <returns>返回字段索引</returns>
	virtual int32			findField(const String& fieldName) const;
	/// <summary>
	/// 获取记录要素集几何体信息的字段
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集几何体信息字段</returns>
	virtual const FD::Utility::DB::Interface::IField*		getGeometryField() const;
	/// <summary>
	/// 获取记录要素集ID信息的字段
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集ID信息字段</returns>
	virtual const FD::Utility::DB::Interface::IField*		getIDField() const;
	/// <summary>
	/// 获取要素集空间索引类型
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集空间索引类型</returns>
	virtual FD::Utility::DB::Interface::SpatialIndexEnum	getSpatialIndexEnum() const;
	/// <summary>
	/// 获取要素集最小外接矩形
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集最小外接矩形</returns>
	virtual FD::Kernel::Geometry::Interface::IEnvelope*			getEnvelopeRef() const;
	/// <summary>
	/// 获取要素集空间参考信息
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集空间参考信息</returns>
	virtual const FD::Kernel::Math::SpatialReference* getSpatialReferenceRef() const;
	/// <summary>
	/// 获取要素集空间参考信息
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回要素集空间参考信息</returns>
	virtual bool setSpatialReference(FD::Kernel::Math::SpatialReference *sr );
	/// <summary>
	/// 通过ID号获取要素
	/// </summary>
	/// <param name="id">ID号</param>
	/// <returns>返回要素</returns>
	virtual   FD::Utility::DB::Interface::IFeature*	getFeature(uint32 id);
	/// <summary>
	/// 通过过滤添加获取要素数量
	/// </summary>
	/// <param name="filter">过滤器</param>
	/// <returns>返回要素数量</returns>
	virtual uint32	getFeatureCount(FD::Utility::DB::Interface::IQueryFilter* filter);
	/// <summary>
	/// 在内存中创建要素
	/// </summary>
	/// <param name=""></param>
	/// <returns>返回创建的要素</returns>
	virtual   FD::Utility::DB::Interface::IFeature*	createFeatureBuffer();
	/// <summary>
	/// 插入要素
	/// </summary>
	/// <param name="feature">要素</param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool		insert(FD::Utility::DB::Interface::IFeature* feature);
	/// <summary>
	/// 插入内存缓冲区要素
	/// </summary>
	/// <param name="buffer">要素</param>
	/// <returns>返回结果代码</returns>
	virtual uint32	insertFeatureBuffer(const FD::Utility::DB::Interface::IFeature* buffer);
	
	/// <summary>
	/// 更新要素
	/// </summary>
	/// <param name="buffer">要素</param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool		update(const FD::Utility::DB::Interface::IFeature* buffer);
	/// <summary>
	///通过ID删除要素
	/// </summary>
	/// <param name="id">ID号</param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool		deleteFeature(uint32 id);
	/// <summary>
	///查询
	/// </summary>
	/// <param name="filter">过滤器</param>
	/// <returns>返回迭代器</returns>
	virtual   FD::Utility::DB::Interface::IFeatureIterator*	query(FD::Utility::DB::Interface::IQueryFilter* filter);
	/// <summary>
	///通过迭代器查询下一条要素
	/// </summary>
	/// <param name="iterator">迭代器</param>
	/// <returns>返回要素</returns>
	  FD::Utility::DB::Interface::IFeature*					nextFeature(const FD::Utility::DB::CFeatureIterator *iterator);
	/// <summary>
	///通过过滤器选择
	/// </summary>
	/// <param name="filter">过滤器</param>
	/// <returns>返回选择集</returns>
	virtual   FD::Utility::DB::Interface::IFeatureSelection*	select(FD::Utility::DB::Interface::IQueryFilter* filter);
	/// <summary>
	///停止指定迭代器的查询
	/// </summary>
	/// <param name="iterator">迭代器</param>
	/// <returns></returns>
	virtual void				endQuery(const FD::Utility::DB::CFeatureIterator *iterator);
	/// <summary>
	///创建空间索引（此接口无效）
	/// </summary>
	/// <param name="spaIndex">空间索引枚举类型</param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool createSpatialIndex(FD::Utility::DB::Interface::SpatialIndexEnum spaIndex);
	/// <summary>
	///创建空间索引
	/// </summary>
	/// <param name="param">空间索引参数类</param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool createSpatialIndex(FD::Utility::DB::Interface::ISpatialIndexParam* param);
	/// <summary>
	///重建空间索引
	/// </summary>
	/// <param name=""></param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool rebulidSpatialIndex();
	/// <summary>
	///删除空间索引
	/// </summary>
	/// <param name=""></param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool deleteSpatialIndex();
	/// <summary>
	/// 获取八叉树索引
	/// </summary>
	/// <returns>返回八叉树索引</returns>
	void* getOCTree() {return (void*)0;}
	/// <summary>
	/// 创建一个数据表的代理对象。
	/// </summary>
	/// <returns>返回数据表</returns>
	virtual   FD::Utility::DB::Interface::ITableProxy* createTableProxy();
	/// <summary>
	/// 添加字段
	/// </summary>
	/// <param name="field">指定字段</param>
	/// <returns>ture，成功；false，失败</returns>
	virtual bool addField(const FD::Utility::DB::Interface::IField* field);
	/// <summary>
	///删除指定的字段
	/// </summary>
	/// <param name="field">指定字段</param>
	/// <returns>若成功返回true；否则返回false</returns>
	virtual bool deleteField(const FD::Utility::DB::Interface::IField* field);
	/// <summary>
	///从流获取要素集信息
	/// </summary>
	/// <param name=""></param>
	/// <returns>若成功返回true；否则返回false</returns>
	bool fromBinary(FD::Kernel::DataStream &stream);
public:
	//CVectorFeatureClass(FD::GraphicEngine::NameValuePairList *pList);
	virtual uint32   insertFeatureBuffers(vector<const FD::Utility::DB::Interface::IFeature*> buffers);
public:
//protected:
	//CVectorFeatureClass();
	CVectorFeatureClass(CVectorFeatureClassOperator* opr);

private:
	virtual FD::Utility::DB::Interface::IFeature*	getFeature2(uint32 id);
	virtual uint32	insertFeatureBuffer2(const FD::Utility::DB::Interface::IFeature* buffer);
	virtual bool		update2(const FD::Utility::DB::Interface::IFeature* buffer);
	virtual bool		deleteFeature2(uint32 id);
	virtual FD::Utility::DB::Interface::IFeatureIterator*	query2(FD::Utility::DB::Interface::IQueryFilter* filter);
	FD::Utility::DB::Interface::IFeature*					nextFeature2(const FD::Utility::DB::CFeatureIterator *iterator);
	virtual FD::Utility::DB::Interface::IFeatureSelection*	select2(FD::Utility::DB::Interface::IQueryFilter* filter);

protected:
	//用于派生类
	virtual void createEditor();
	virtual void saveEditor();
	virtual void clearEditor();

	/* 数据集名字 */
	String						m_Name;
	/* 别名，一般不用 */
	String						m_AliasName;
	/* 数据集类型 */
	FD::Utility::DB::Interface::DatasetType				m_nType;
	/* 描述信息 */
	String						m_Description;
	/* 数据集的空间坐标系 */
	FD::Kernel::ref_ptr<FD::Kernel::Math::SpatialReference>		m_pSpatialReference;
	/* 数据集的范围 */
	FD::Kernel::Geometry::Interface::IEnvelope*					m_pDatasetExtent;
	/* 几何体字段名称 */
	String						m_ShapeFieldName;
	/* 几何类型 */
	FD::Kernel::Geometry::Interface::GeometryType				m_nGeometryType;
	///* 字段 */
	FD::Utility::DB::Interface::IFields*					m_pFieldSet;
	///* 空间索引类型 */
	FD::Utility::DB::Interface::SpatialIndexEnum			m_nSpatialIndexType;

	std::list<FD::Utility::DB::CFeatureIterator*>mQueryIterators;
	
	//mQueryIterators
	FD::Kernel::RecursiveMutex mLock;
	
	bool						m_IsEditing;
	bool						m_IsEditingOperation;
	bool						m_bUndo;

	uint32					m_maxID;
	//
	CVectorFeatureClassOperator* m_impl;
	CVectorFeatureClassEditor	*m_pEditor;

};






}}} // End of Namespaces

#endif //_VECTOR_FEATURE_CLASS_H_H_

